package 集合框架;

import java.util.*;

public class Demo3 {
    // 1. 如何产生对象
    public static void createArrayList(){
        ArrayList<Integer> list1 = new ArrayList<>();
        list1.add(10);
        list1.add(1,20);
        List<String> list2 = new ArrayList<>();
        ArrayList<String> list3 = new ArrayList<>(list2);
        System.out.println(list1);
        System.out.println(list2);
        System.out.println(list3);
    }
    // 2. 如何对集合进行CRUD(存放、获取、修改、删除)
    public static void crudArrayList(){
        // 创建集合
        ArrayList<Integer> list1 = new ArrayList<>();
        // 存放元素
        for (int i = 0; i < 20; i++){
            list1.add((int)(Math.random()*20+1));
        }
        System.out.println(list1);
        // 获取元素
        Integer i = list1.get(1);
        System.out.println(i);
        // 修改元素
        list1.set(2, 30);
        System.out.println(list1);
        // 删除元素
        list1.remove(1);
        System.out.println(list1);
        list1.clear();
        System.out.println(list1);
    }
    // 3. 其他方法
    public static void otherMethod(){
        List<String> list = new ArrayList<>();
        list.add("aa");
        list.add("bb");
        list.add("cc");
        list.add("dd");
        list.add("ee");
        list.add("ff");
        list.add(null);
        System.out.println(list);
        int size = list.size();
        System.out.println(size);
        List<String> subList = list.subList(1, 4);
        System.out.println(subList);
        Object[] array = list.toArray();
        System.out.println(Arrays.toString(array));
        boolean isEmpty = list.isEmpty();
        System.out.println(isEmpty);
    }
    // 4. 循环集合
    public static void loopArrayList(){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add((int) (Math.random() * 100 + 1));
        }
        System.out.println(list);
        //for循环遍历集合
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i)+"\t");
        }
        System.out.println();
        //forEach循环遍历集合(缩写版)
        for(Integer i : list){
            System.out.print(i+"\t");
        }
        System.out.println();
        //forEach循环遍历集合(完整版)
        Iterator<Integer> iterator = list.iterator();
        while(iterator.hasNext()){
            System.out.print(iterator.next()+"\t");
        }
        System.out.println();
        //lambda表达式遍历集合
        list.forEach(i -> System.out.print(i + "\t"));
        System.out.println();

    }

    public static Scanner scanner = new Scanner(System.in);
    // 5. 如何排序、如何查找
    public static void ArrayListSearch(){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add((int) (Math.random() * 20 + 1));
        }
        System.out.println(list);
        System.out.print("请输入要查找的元素:");
        int key = scanner.nextInt();
        boolean b = list.contains(key);
        System.out.println(b);
        int index1 = list.indexOf(key);
        int index2 = list.lastIndexOf(key);
        System.out.println(index1);
        System.out.println(index2);
    }

    public static void ArrayListSort(){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add((int) (Math.random() * 20 + 1));
        }
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);
    }


    public static void main(String[] args) {
//        createArrayList();
//        crudArrayList();
//        otherMethod();
//        loopArrayList();
        ArrayListSearch();
        ArrayListSort();
    }
}
